Thursday, May 10, 2007

Music.java

Music.java

public class Music {
public static void main(String[] args) {
Guitar g = new Guitar();
Piano p = new Piano();

tune(g);
tune(p);
}

static public void tune(Instrument i) {
i.play();

// the following is illegal.
// i.gPlay();
}
}

class Instrument {
public void play() {
System.out.println("Instrument.play()");
}
}

class Guitar extends Instrument {
public void gPlay() {
System.out.println("Guitar.gPlay()");
}
}

class Piano extends Instrument {
}

Music.java output

Instrument.play()
Instrument.play()


Tag: Study Code Program Java

No comments:

Post a Comment